package com.nit.lab.mapper.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.UserDTO;
import com.nit.lab.entity.basic.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    /**
     * @Description: 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT " +
            "user_id AS `key`, user_id, " +
            "login_name, role_id, user_name, user_type, " +
            "user_info, basic_user.gmt_create, login_pwd " +
            "FROM basic_user " +
            "WHERE TRUE " +
            "<if test='loginName != null'> AND login_name LIKE CONCAT('%', #{loginName}, '%') </if>" +
            "<if test='userName != null'> AND user_name LIKE CONCAT('%', #{userName}, '%') </if>" +
            "<if test='roleId != null'> AND role_id = #{roleId} </if>" +
//            "<if test='unitId != null'> AND basic_user.unit_id = #{unitId} </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "user_type desc, user_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<UserDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("loginName") String loginName,
            @Param("userName") String userName,
            @Param("roleId") String roleId,
//            @Param("unitId") String unitId,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /**
     * @Description: 获取表格数据记录的总条数
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_user " +
            "WHERE TRUE " +
            "<if test='loginName != null'> AND login_name LIKE CONCAT('%', #{loginName}, '%') </if>" +
            "<if test='userName != null'> AND user_name LIKE CONCAT('%', #{userName}, '%') </if>" +
            "<if test='roleId != null'> AND role_id = #{roleId} </if>" +
//            "<if test='unitId != null'> AND basic_user.unit_id = #{unitId} </if>" +
            "</script>"})
    Long count4Table(
            @Param("loginName") String loginName,
            @Param("userName") String userName,
            @Param("roleId") String roleId
//            @Param("unitId") String unitId
    ) throws Exception;

    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Insert("INSERT INTO basic_user (" +
            "login_name, login_pwd, " +
            "user_name, user_type, user_info, " +
            "role_id, " +
            "gmt_create, gmt_modified) " +
            "VALUES (#{userEntity.loginName}, #{userEntity.loginPwd}, " +
           "#{userEntity.userName}, #{userEntity.userType}, " +
            "#{userEntity.userInfo}, #{userEntity.roleId}, " +
            "#{userEntity.gmtCreate}, #{userEntity.gmtModified})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    Integer add(@Param("userEntity") UserEntity userEntity) throws Exception;

    /**
     * @Description: 更新记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Update("UPDATE basic_user " +
            "SET login_name = #{userEntity.loginName}, " +
            "user_name = #{userEntity.userName}, " +
            "user_type = #{userEntity.userType}, " +
            "user_info = #{userEntity.userInfo}, " +
            "role_id = #{userEntity.roleId}, " +
            "gmt_modified = #{userEntity.gmtModified} " +
            "WHERE user_id = #{userEntity.userId}")
    Integer update(@Param("userEntity") UserEntity userEntity) throws Exception;


    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Delete("DELETE FROM basic_user WHERE user_id = #{userId}")
    Integer remove(@Param("userId") Long userId) throws Exception;

    /**
     * @Description: 重置用户密码
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Update({"UPDATE basic_user " +
            "SET login_pwd = #{loginPwd} " +
            "WHERE user_id = #{userId}"
    })
    Integer resetPwd(@Param("userId") Long userId,
                 @Param("loginPwd") String loginPwd) throws Exception;


    /**
     * @Description: 根据用户登录名称获取用户信息
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"SELECT " +
            "user_id, login_name, role_id, " +
            "login_pwd, user_name, " +
            "user_type, user_info, " +
            "basic_user.gmt_create, gmt_modified " +
            "FROM basic_user " +
            "WHERE login_name = #{loginName} LIMIT 1"
    })
    UserDTO getByLoginName(@Param("loginName") String loginName) throws Exception;

    /**
     * @Description: 根据用户ID获取用户信息
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"SELECT " +
            "user_id, login_name, role_id, " +
            "login_pwd, user_name, user_type, " +
            "user_info, " +
            "gmt_create, gmt_modified " +
            "FROM basic_user " +
            "WHERE user_id = #{userId}"
    })
    UserDTO getByUserId(@Param("userId") Long userId) throws Exception;

    /**
     * @Description: 更新用户状态
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Update("UPDATE basic_user " +
            "set user_type=case when user_type=0 then 1 else 0 end " +
            "WHERE user_id = #{userId}")
    Integer updateUserType(@Param("userId") Long userId) throws Exception;

    @Select("select * " +
            "from basic_user " +
            "where user_type = 1 ")
    List<UserDTO> getUserListAll() throws Exception;

    /**
     * @Description: 根据用户userName获取用户信息
     * @Author: LYN
     * @Date: 2022/07/12
     */
    @Select({"SELECT " +
            "user_id, login_name, role_id, " +
            "login_pwd, user_name, user_type, public_key, private_key, " +
            "user_info, " +
            "gmt_create, gmt_modified " +
            "FROM basic_user " +
            "WHERE user_Name = #{userName}"
    })
    List<UserDTO> getByUserName(@Param("userName") String userName) throws Exception;

    /**
     * @Description: 根据用户openid获取用户信息
     * @Author: LYN
     * @Date: 2022/07/12
     */
    @Select({"SELECT " +
            "user_id, login_name, role_id, " +
            "login_pwd, user_name, public_key, private_key, " +
            "user_info, user_unit, " +
            "gmt_create, gmt_modified " +
            "FROM basic_user " +
            "WHERE open_id = #{openid}"
    })
    List<UserDTO> getByOpenId(@Param("openid") String openId) throws Exception;
    @Insert("INSERT INTO basic_user (" +
            "login_name, login_pwd, " +
            "user_name, user_type, user_info, public_key, private_key, open_id, " +
            "role_id, " +
            "gmt_create, gmt_modified , user_unit) " +
            "VALUES (#{userEntity.loginName}, #{userEntity.loginPwd}, " +
            "#{userEntity.userName}, 1, " +
            "#{userEntity.userInfo}, " +
            "#{userEntity.publicKey}, #{userEntity.privateKey}, " +
            "#{userEntity.openId}, 2, " +
            "#{userEntity.gmtCreate}, #{userEntity.gmtModified} , #{userEntity.userUnit})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    Integer weiAdd(@Param("userEntity") UserEntity userEntity) throws Exception;

    /**
     * @Description: 自动在数据库添加当前时间的tokenid
     * @Author: LYN
     * @Date: 2022/07/12
     */
    @Update("UPDATE weixin_token " +
            "set accsee_token = #{tokenUrl}")
    void setTokenByTime(@Param("tokenUrl") String tokenUrl) throws Exception;

    @Select({"SELECT " +
            "accsee_token " +
            "FROM weixin_token "
    })
    String getweixinToken() throws Exception;
    /**
     * @Description: 判断是否有准考证号
     * @Author: LYN
     * @Date: 2022/07/12
     */
    @Select("select check_name " +
            "from check_user " +
            "where check_admiss = #{checkAdmiss} ")
    String checkAdmiss(String checkAdmiss) throws Exception;

    /**
     * @Description: 根据准考证号返回姓名
     * @Author: LYN
     * @Date: 2022/07/12
     */
    @Select("select check_name " +
            "from check_user " +
            "where check_admiss = #{checkAdmiss} ")
    String getCheckName(String checkAdmiss) throws Exception;
    /**
     * @Description: 根据准考证号返回学院
     * @Author: LYN
     * @Date: 2022/07/12
     */
    @Select("select check_unit " +
            "from check_user " +
            "where check_admiss = #{checkAdmiss} ")
    String getCheckUnit(String checkAdmiss) throws Exception;

    /**
     * @Description: 根据区块链号返回
     * @Author: LYN
     * @Date: 2022/07/17
     */
    @Select("select login_name, user_name " +
            "from basic_user " +
            "where user_info = #{userInfo} ")
    JSONObject getCheckAddress(String userInfo) throws Exception;
}